package com.example.suuu.project.system.entity.vo;

import com.example.suuu.project.system.entity.SuUser;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * security 权限
 */
public class MyUserDetails  implements UserDetails {
    /**
     * 登录用户信息
     */
    private SuUser user;
    /**
     * 登录用户权限
     */
    private List<String> permissions;

    public MyUserDetails(SuUser user, List<String> permissions) {
        this.user = user;
        this.permissions = permissions;
    }


    public SuUser getUser() {
        return user;
    }
    public List<String> getPermissions() {
        return permissions;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return permissions.stream()
                .filter(permission -> permission != null)
                .map(permission -> new SimpleGrantedAuthority(permission))
                .collect(Collectors.toList());
    }

    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return user.getStatus().equals(1);
    }
}
